SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[SP_INSERT_USER](@PERSON_ID BIGINT,@USER VARCHAR(25),@PASS VARCHAR(25))  AS
DECLARE @PASSWORD VARBINARY(256)

IF NOT EXISTS(SELECT 1 FROM dbo.T_PERSON WHERE PERSON_ID=@PERSON_ID)
BEGIN
		SELECT 'NO SE ENCONTRO INFORMACION DE LA PERSONA'
		RETURN 0
	END



IF NOT EXISTS(SELECT 1 FROM .dbo.T_USER WHERE PERSON_ID=@PERSON_ID)
	BEGIN
		OPEN SYMMETRIC KEY NDEV_PASSWORD DECRYPTION
	    BY CERTIFICATE NDEV
		SET @PASSWORD = EncryptByKey(Key_GUID('NDEV_PASSWORD'), @PASS)
		INSERT INTO dbo.T_USER(PERSON_ID,USER_LOGIN,USER_PASSWORD,CREATE_DATE,USER_STATUS)
		VALUES(@PERSON_ID,@USER,@PASSWORD,GETDATE(),1)

		CLOSE SYMMETRIC KEY NDEV_PASSWORD

		SELECT 'SUCCESS'
		RETURN 1
	END
ELSE 
	BEGIN
	SELECT 'USUARIO EXISTENTE'
	RETURN 0
	END
GO
